package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.base.WebController;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.helper.ConvertHelper;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.helper.Helper;
import com.xci.core.web.JsonGrid;
import com.xci.core.web.TreeNode;
import com.xci.sys.entity.SysDept;
import com.xci.sys.entity.SysModule;
import com.xci.sys.entity.SysRole;
import com.xci.sys.filter.RoleFilter;
import com.xci.sys.service.DeptService;
import com.xci.sys.service.ModuleService;
import com.xci.sys.service.RoleService;
import com.xci.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统角色控制器
 * @author lvyanyang
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/sys/role")
public class RoleController extends WebController {
    @Resource
    private UserService userService;//用户服务
    @Resource
    private DeptService deptService;//部门服务
    @Resource
    private RoleService roleService;//角色服务
    @Resource
    private ModuleService moduleService;//模块服务
    //region 页面视图

    /**
     * 角色首页
     */
    @GetMapping
    @Authorize(code = "SystemRole")
    public String index() {
        return "sys/role/index";
    }

    /**
     * 角色新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemRole.Create")
    public String create(ModelMap map) {
        createMark(map);
        SysRole entity = new SysRole();
        entity.setId(Helper.nextIdStr());
        entity.setStatus(true);
        map.put("entity", entity);
        return "sys/role/edit";
    }

    /**
     * 角色编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemRole.Edit")
    public String edit(String id, ModelMap map) {
        SysRole entity = roleService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);

        map.put("entity", entity);
        return "sys/role/edit";
    }

    /**
     * 角色详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        SysRole entity = roleService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        
        map.put("entity", entity);
        map.put("users", userService.selectListByRoleId(id));
        //map.put("ownDataSetting", permissionService.queryObjectDataSetting(id));
        return "sys/role/details";
    }

    /**
     * 角色拥有的模块 tree 节点
     *
     * @param id 角色主键
     */
    @ResponseBody
    @GetMapping("/role-own-modules")
    public RestMessage roleOwnModules(String id) {
        List<SysModule> list = roleService.selectModuleMapList(id);
        List<TreeNode> models = moduleService.convertToNodeList(list);
        return RestMessage.tree(models);
    }

    /**
     * 角色拥有的部门 tree 节点
     *
     * @param id 角色主键
     */
    @ResponseBody
    @GetMapping("/role-own-departments")
    public RestMessage roleOwnDepartments(String id) {
        List<SysDept> departments = roleService.selectDeptMapList(id);
        return RestMessage.tree(deptService.convertToNodeList(departments));
    }

    //endregion

    //region 数据处理

    /**
     * 角色表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemRole.Search")
    public JsonGrid grid(RoleFilter filter) {
        return RestMessage.grid(roleService.selectPageList(filter));
    }

    /**
     * 保存新增角色
     */
    @ResponseBody
    @PostMapping("/createSave")
    @Authorize(code = "SystemRole.Create")
    public RestMessage createSave(@ModelAttribute SysRole entity) {
        return roleService.insert(entity);
    }

    /**
     * 保存修改角色
     */
    @ResponseBody
    @PostMapping("/updateSave")
    @Authorize(code = "SystemRole.Edit")
    public RestMessage editSave(@ModelAttribute SysRole entity) {
        return roleService.update(entity);
    }

    /**
     * 修改角色启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemRole.Status")
    public RestMessage status(String id, Integer status) {
        return roleService.updateStatus(id, ConvertHelper.toBool(status));
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemRole.Delete")
    public RestMessage delete(String ids) {
        return roleService.delete(ids);
    }

    /**
     * 导出角色
     */
    @GetMapping("/export")
    @Authorize(code = "SystemRole.Export")
    public void export(RoleFilter filter) {
        ExcelHelper.exportWeb(roleService.selectList(filter), SysRole.class, "系统角色");
    }

    //endregion
}